package com.it.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController
@RequestMapping("/files")
public class FileController {


    @Value("${ip:localhost}")
    String ip;

    private static final String ParenPath = System.getProperty("user.dir") + File.separator +"files";

    @PostMapping("/SingerImgUpload")
    public String SingerImgUpload(MultipartFile file){
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "SingerImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ ip +":9000/files/images/SingerImg/" + OriginFilename;
    }


    @PostMapping("/UserImgUpload")
    public String UerImgUpload(MultipartFile file,HttpServletRequest request){

        String token = request.getHeader("token");
        System.out.println("token*******++++==="+token);
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "UserImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ip+":9000/files/images/UserImg/" + OriginFilename;
    }

    @PostMapping("/SongImgUpload")
    public String SongImgUpload(MultipartFile file){
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "SongImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ip+":9000/files/images/SongImg/" + OriginFilename;
    }

    @PostMapping("/SongListImgUpload")
    public String SongListImgUpload(MultipartFile file){
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "SongListImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ ip + ":9000/files/images/SongListImg/" + OriginFilename;
    }

    @PostMapping("/SongUpload")
    public String SongUpload(MultipartFile file){


        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "Songs";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ip+":9000/files/Songs/" + OriginFilename;
    }

    //下载
    @PostMapping("/download")
    public void download(@RequestParam("filename") String filename, HttpServletResponse response,HttpServletRequest request) throws IOException {

        String filepath = ParenPath + File.separator + "Songs" + File.separator + filename;
        //输入流读取文件
        FileInputStream files = new FileInputStream(filepath);
        byte[] b =new byte[1024];
        //输出流输出文件
        ServletOutputStream os = response.getOutputStream();

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));

        int len = -1;
        while((len = files.read(b) )!= -1){
            os.write(b,0, len);
            os.flush();
        }
        os.close();
        files.close();
    }

    //预览
    @GetMapping("/show")
    public void show(@RequestParam("filename") String filename, HttpServletResponse response) throws IOException {

        String filepath = ParenPath + File.separator + "Songs" + File.separator + filename;
        //输入流读取文件
        FileInputStream files = new FileInputStream(filepath);
        byte[] b =new byte[1024];
        //输出流输出文件
        ServletOutputStream os = response.getOutputStream();

        int len = -1;
        while((len = files.read(b) )!= -1){
            os.write(b,0, len);
            os.flush();
        }
        os.close();
        files.close();
    }

    @PostMapping("/ArticleImgUpload")
    public Dict ArticleImgUpload(MultipartFile file){
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "ArticleImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String url = "http://"+ip+":9000/files/images/ArticleImg/" + OriginFilename;
        return Dict.create().set("error",0).set("data", CollUtil.newArrayList(Dict.create().set("url",url)));
    }

    @PostMapping("/ArticleCoverImgUpload")
    public String ArticleCoverImgUpload(MultipartFile file){
        System.out.println(ParenPath);
        String OriginFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(OriginFilename);
        String exName = FileUtil.extName(OriginFilename);
        System.out.println(mainName);
        System.out.println(exName);
        String projectPath = ParenPath + File.separator + "images" + File.separator + "ArticleCoverImg";
        System.out.println(projectPath);
        File saveFile = new File(projectPath);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        String FileRealpath = projectPath + File.separator + OriginFilename;
        File savefile = new File(FileRealpath);
        if(savefile.exists()){
            OriginFilename = System.currentTimeMillis() + "_" + mainName + "." + exName;
            savefile = new File(projectPath + File.separator + OriginFilename);
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://"+ip+":9000/files/images/ArticleCoverImg/" + OriginFilename;
    }
}
